package com.nsu.tly.web;

import com.nsu.tly.entity.User;
import com.nsu.tly.service.UserService;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.io.IOException;
import java.util.List;

/**
 * @Description:
 * @Author: Elder Tang
 * @Date: 2021/7/20 16:21
 */
@Controller
@RequestMapping("/user")
public class UserController {
    @Autowired
    private UserService userService;

    private final static Logger logger = LoggerFactory.getLogger(UserController.class);

    @ResponseBody
    @RequestMapping("/findAll")
    public List<User> findAll(){
        return userService.selectAll();
    }


    @RequestMapping("/main")
    public String toMain(){



        logger.trace("logback的--trace日志--输出了");
        logger.debug("logback的--debug日志--输出了");
        logger.info("logback的--info日志--输出了");
        logger.warn("logback的--warn日志--输出了");
        logger.error("logback的--error日志--输出了");





        return "main";
    }

    @RequestMapping("/login")
    public String toLogin(){
        return "login";
    }

    @RequestMapping("/page")
    public String toPage(){
        return "page";
    }

/*
    @RequestMapping("/login")
    public String toLogin(){
        return "login";
    }

    @RequestMapping("/main")
    public String toMain(){
        return "main";
    }



    @RequestMapping("/loginCheck")
    public String loginCheck(HttpServletRequest request, Model model,
                           HttpSession session, String uname, String upass){
        if("admin".equals(uname)&&"123".equals(upass)){
            //model.addAttribute("user",uname);
            session.setAttribute("user",uname);
            return "main";
        }else{
            model.addAttribute("msg","用户名或密码错误！");
            return "login";
        }
    }

    @RequestMapping("/t6/{page}")
    public String t6(@PathVariable("page")String page){
        return page;
    }*/

    @RequestMapping("/loginCheck")
    public void loginCheck(String uname, String upass, HttpSession session, HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        if(userService.loginCheck(uname,upass)){
            session.setAttribute("user", uname);
            //request.setAttribute("msg","你还没有登录，请先登录再操作！");
            request.getRequestDispatcher("/WEB-INF/jsp/main.jsp").forward(request,response);
        }else{
            request.setAttribute("msg","用户名或密码有误，请重新登录！");
            request.getRequestDispatcher("/WEB-INF/jsp/login.jsp").forward(request,response);
        }
    }

}
